import request from '../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    videoGroupList:[], //导航标签数据
    navId:'', //导航的标识,
    videoList:[], //视频的列表区域
    videoId:'', //图片id
    videoUpdateTime:[], //记录视频播放的时长
    isTriggered:false,  //标识下拉刷新状态
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getVideoGrounpListData()
  },
  async getVideoGrounpListData(){
    let videoGrounpListData = await request('/video/group/list')
    this.setData({
      videoGroupList:videoGrounpListData.data.slice(0,14),
      navId:videoGrounpListData.data[0].id
    })
    this.getVideoList(this.data.navId)
  },
  //获取视频列表数据
  async getVideoList(navId){
    let videoListData = await request('/video/group',{id:navId})
    // 关闭消息提示框
    wx.hideLoading()
    let index = 0
    let videoList = videoListData.datas.map(item =>{
      item.id =  index++
      return item
    })
    this.setData({
      videoList:videoList,
      //关闭下拉刷新
      isTriggered:false
    })
  },
  //切换导航回调
  changeNav(e){
    let navId = e.currentTarget.id;
    this.setData({
      navId:navId*1,
      videoList:[]

    })
    // 显示正在加载
    wx.showLoading({
      title: '正在加载',
    })
    // 动态获得当前导航对应的视频数据
    this.getVideoList(this.data.navId)
  },
  // 点击播放/继续播放
  handlePlay(e){
    let vid = e.currentTarget.id;
    //关闭上一个视频
    this.vid !== vid && this.videoContext && this.videoContext.stop();
    this.vid = vid;
    // 更新图片id
    this.setData({
      videoId:vid
    })
    //创建控制video标签的实例对象
    this.videoContext = wx.createVideoContext(vid);
    //判断
    let {videoUpdateTime} = this.data;
    let videoItem = videoUpdateTime.find(item => item.vid === vid);
    if(videoItem){
      this.videoContext.seek(videoItem.currentTime)
    }
  },
  //监听视频播放的时长
  handleTimeUpdate(e){
    let videoTimeobj = {vid:e.currentTarget.id,currentTime:e.detail.currentTime}
    let {videoUpdateTime} = this.data;
    let videoItem = videoUpdateTime.find(item => item.vid === videoTimeobj.vid);
    if(videoItem){
      videoItem.currentTime = videoTimeobj.currentTime;
    }else{
      videoUpdateTime.push(videoTimeobj)
    }
    //更新
    this.setData({
      videoUpdateTime
    })
  },
  //视频结束调用
  handleEnded(e){
    //移出记录播放时长数组当前数组
    let {videoUpdateTime} = this.data;
    videoUpdateTime.splice(videoUpdateTime.findIndex(item => item.vid === e.currentTarget.id),1)
    this.setData({
      videoUpdateTime
    })
  },
  handleRefresher(){
    this.getVideoList(this.data.navId)
  },
  toSearch(){
    wx.navigateTo({
      url: '/pages/search/search',
    })
  },
  //上拉加载更多
  handleToLow(){
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function ({
    from
  }) {
    console.log(from)
    if (from === 'button') {
      return {
        title: '自定义转发内容',
        page: '/pages/video/video',
        imageUrl: '/static/images/personal/bgImg.jpg'
      }
    }else{
      return{
        title:'来自menu的转发',
        page:'/pages/video/video',
        imageUrl: '/static/images/personal/bgImg.jpg'
      }
    }
  }
})